<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<!-- CALL WITH THIS
	<xsl:param name="tomorrow">
		<xsl:call-template name="next-day">
			<xsl:with-param name="date" select="$today"/>
		</xsl:call-template>
	</xsl:param>
-->


<xsl:template name="month-days">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:param name="month-int" select="format-number($month,'0')"/>
	<xsl:param name="leap-year">
		<xsl:call-template name="is-leap-year">
			<xsl:with-param name="year" select="$year"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:choose>
		<xsl:when test="$month-int!=2">
			<xsl:value-of select="/data/calendar/year/month[@value=$month-int]/@days"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$leap-year!=1">
					<xsl:value-of select="/data/calendar/year/month[@value=2]/@days"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="/data/calendar/year/month[@value=2]/@leap-year-days"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template name="previous-month">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:choose>
		<xsl:when test="$month - 1 &lt; 1">12</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="format-number($month - 1,'00')"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template name="next-month">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:choose>
		<xsl:when test="$month + 1 &gt; 12">01</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="format-number($month + 1,'00')"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template name="previous-month-year">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:choose>
		<xsl:when test="$month != 1">
			<xsl:value-of select="$year"/>
		</xsl:when>
		<xsl:when test="$month = 1">
			<xsl:value-of select="$year - 1"/>
		</xsl:when>
		<xsl:otherwise>0</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template name="next-month-year">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:choose>
		<xsl:when test="$month &lt; 12">
			<xsl:value-of select="$year"/>
		</xsl:when>
		<xsl:when test="$month = 12">
			<xsl:value-of select="$year + 1"/>
		</xsl:when>
		<xsl:otherwise>0</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template name="next-day">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:param name="day" select="substring($date,9,2)"/>
	<xsl:param name="month-days">
		<xsl:call-template name="month-days">
			<xsl:with-param name="year" select="$year"/>
			<xsl:with-param name="month" select="$month"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:param name="next-month">
		<xsl:call-template name="next-month">
			<xsl:with-param name="month" select="$month"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:param name="next-month-year">
		<xsl:call-template name="next-month-year">
			<xsl:with-param name="year" select="$year"/>
			<xsl:with-param name="month" select="$month"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:choose>
		<xsl:when test="$day + 1 &gt; $month-days">
			<xsl:value-of select="concat($next-month-year,'-',$next-month,'-01')"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="concat($year,'-',$month,'-',format-number($day + 1,'00'))"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template name="previous-month-days">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:param name="previous-month">
		<xsl:call-template name="previous-month">
			<xsl:with-param name="month" select="$month"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:param name="leap-year">
		<xsl:call-template name="is-leap-year">
			<xsl:with-param name="year" select="$year"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:call-template name="month-days">
		<xsl:with-param name="leap-year" select="$leap-year"/>
		<xsl:with-param name="month" select="$previous-month"/>
	</xsl:call-template>
</xsl:template>
<xsl:template name="next-month-days">
	<xsl:param name="date"/>
	<xsl:param name="year" select="substring($date,1,4)"/>
	<xsl:param name="month" select="substring($date,6,2)"/>
	<xsl:param name="next-month">
		<xsl:call-template name="next-month">
			<xsl:with-param name="month" select="$month"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:param name="leap-year">
		<xsl:call-template name="is-leap-year">
			<xsl:with-param name="year" select="$year"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:call-template name="month-days">
		<xsl:with-param name="leap-year" select="$leap-year"/>
		<xsl:with-param name="month" select="$next-month"/>
	</xsl:call-template>
</xsl:template>
<xsl:template name="week-number">
	<xsl:param name="date" select="$today"/>
	<xsl:param name="year" select="substring($date, 1, 4)" />
	<xsl:param name="leap" select="(not($year mod 4) and $year mod 100) or not($year mod 400)" />
	<xsl:param name="month" select="substring($date, 6, 2)" />
	<xsl:param name="day" select="substring($date, 9, 2)" />
	<xsl:param name="month-days">
		<xsl:call-template name="month-days">
			<xsl:with-param name="leap-year" select="$leap"/>
			<xsl:with-param name="month" select="$month"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:param name="days">
		<xsl:choose>
			<xsl:when test="$leap and $month > 2">
				<xsl:value-of select="$month-days + $day + 1" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$month-days + $day" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<xsl:param name="y-1" select="$year - 1" />
	<xsl:param name="day-of-week" 

      select="(($y-1 + floor($y-1 div 4) -
           floor($y-1 div 100) + floor($y-1 div 400) +
           $days) 
           mod 7) + 1" />
	<xsl:choose>
		<xsl:when test="($day - $day-of-week) mod 7">
			<xsl:value-of select="floor(($day - $day-of-week) div 7) + 2" />
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="floor(($day - $day-of-week) div 7) + 1" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>